package com.baidumap.administrator.happystudy.ui;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import com.ab.dialog.loadingDialog.LoadingDialogUtil;
import com.ab.http.OnMessageResponse;
import com.ab.util.AbStrUtil;
import com.ab.util.ToastUtil;
import com.ab.view.clear_edit_text.ClearEditText;
import com.baidumap.administrator.happystudy.R;
import com.baidumap.administrator.happystudy.api.Api;
import com.baidumap.administrator.happystudy.base.AppBaseActivity;
import com.baidumap.administrator.happystudy.base.Constant;
import com.baidumap.administrator.happystudy.model.SendSMS;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 描述：注册
 * 作者：xiaoxing on 17/4/7 15:32
 * 邮箱：2235445233@qq.com
 */
public class RegisterActivity extends AppBaseActivity implements OnMessageResponse {
    @Bind(R.id.cet_tel)
    ClearEditText cetTel;
    @Bind(R.id.tv_xie_yi)
    TextView tvXieYi;
    @Bind(R.id.cb_agree)
    CheckBox cbAgree;
    private String mTel;

    @Override
    public int bindLayout() {
        return R.layout.activity_register;
    }

    @Override
    public void initView(View view) {
        super.initView(view);
    }

    @Override
    public void doBusiness(Context mContext) {
        super.doBusiness(mContext);
        setHeaderBack();
        setHeaderTitle(R.string.register);
    }

    @OnClick({R.id.btn_next_step, R.id.tv_xie_yi})
    public void onClick(View view) {

        switch (view.getId()) {

            case R.id.btn_next_step:
                next();
                break;
            case R.id.tv_xie_yi:
                startActivity(XieYiActivity.class);
                break;

        }
    }

    /**
     * 下一步
     */
    private void next() {

        if (checkTelCheckBox()) {

            LoadingDialogUtil.showGifdialog(getSupportFragmentManager());
            Api.sendSms(this, mTel);


        }
    }

    private boolean checkTelCheckBox() {
        mTel = getEidtTextValue(cetTel);

        if (AbStrUtil.isEmpty(mTel)) {
            ToastUtil.showMessage(this, "手机号不能为空");
            return false;
        } else if (!AbStrUtil.isMobileNo(mTel)) {
            ToastUtil.showMessage(this, "手机号格式错误");
            return false;
        } else if (!cbAgree.isChecked()) {
            ToastUtil.showMessage(this, "请先阅读并同意学习乐使用条款及隐私协议");
            return false;
        }
        return true;
    }

    @Override
    public void onMessageResponse(String url, JSONObject jo) throws JSONException {

        if (url.equals(Constant.SEND_SMS) && jo != null) {
            SendSMS sendSMS = new SendSMS(jo.toString());

            if (sendSMS != null) {


                if (sendSMS.getCode().equals("200")) {

                    ToastUtil.showMessage(this, "验证码发送成功");

                    Bundle bundle = new Bundle();
                    bundle.putString("tel", mTel);
                    bundle.putString("type", "register");

                    startBundleActivity(bundle, SetPwdActivity.class);
                }else {
                    ToastUtil.showMessage(this, sendSMS.getMsg());

                }
            }

        }

    }

    @Override
    public void onMessageResponse(String url, JSONArray jo) throws JSONException {

        ToastUtil.showMessage(this,"JSONArray");
    }

    @Override
    public void onMessageResponse(String url, String str) throws Exception {
        ToastUtil.showMessage(this,"str");
    }
}
